<?php
/*
 ** @introduction: 商品发布修改等...
 ** @author: 杨陈鹏
 ** @date: 2015-04-8 11:37
 ** @email: yangchenpeng@cdlinglu.com
 */

namespace Admin\Controller\Goods;


use Common\Controller\Admin\CommonController;

class GoodsController extends CommonController
{
    /*
     ** @introduction: 商品列表
     ** @author: 杨陈鹏
     ** @date: 2015-04-12 55:37
     ** @email: yangchenpeng@cdlinglu.com
     */
    public function lists()
    {

        $logic = D('Common/Goods', 'Logic');

        $categoryLogic = D('Common/category', 'Logic');

        $this->page = I('post.page',1);

        $this->category_id = I('param.category_id',0);

        $this->status = I('param.status',false);

        $where = '';

        $this->nums = 20;

        if ($this->category_id) {

            //获取指定分类下的所有子分类ID

            $category_ids = $categoryLogic::getChilds($this->category_id, 'id');

            $category_ids_str = '';
            foreach ($category_ids as $v)
                $category_ids_str .= $v['id'] . ',';

            $category_ids_str = $category_ids_str . $this->category_id;

            $category_ids_str = rtrim($category_ids_str, ',');

            $where .= "category_id IN($category_ids_str)";

        }

        if($where)
            $where .= ' AND ';

        switch($this->status){


            //出售中
            case 1:

                $where .= 'goods_state = 1 AND publish_time < '.NOW_TIME;

                break;

            //仓库中
            case 2:

                $where .= 'goods_state = 0';

                break;

            //待发布
            case 3:

                $where .= 'goods_state = 1 AND publish_time > '.NOW_TIME;

                break;

            //回收站
            case 4:

                $where .= 'goods_state = 2';

                break;

            default:

                $where .= 'goods_state != 2';

                break;


        }

        $data = $logic::getData($where, $this->page, '*', $orders = ['column' => 'add_time', 'type' => 'desc'], $this->nums);

        $this->data = $data['data'];

        $this->total = $data['total'];

        $logic = D('Common/Category', 'Logic');
        //获取所有分类
        $categorys = $logic::getChilds(0, 'id,name,pid');

        //获取树型下拉框
        $tree = new \Org\Util\Tree();

        $tree->init($categorys);

        $this->category_tree = $tree->create(0, '<option $selected value=\'$id\'>$spacer $name</option >',$this->category_id);


        $this->display();

    }

    /*
     ** @introduction: 发布商品
     ** @author: 杨陈鹏
     ** @date: 2015-04-12 55:37
     ** @email: yangchenpeng@cdlinglu.com
     */
    public function create()
    {

        $data = I('post.data', '', false);

        if ($data) {

            $logic = D('Common/Goods', 'Logic');

            $this->ajaxReturn($logic::create($data));

        } else {

            $logic = D('Common/Category', 'Logic');
            //获取所有规则
            $categorys = $logic::getChilds(0, 'id,name,pid,child');

            //获取树型下拉框
            $tree = new \Org\Util\Tree();

            $tree->init($categorys);

            $this->category_tree = $tree->create(0, '<option value=$id $disabled>$spacer $name</option>', 0, '', true);

            $this->display();

        }

    }

    /*
     ** @introduction: 修改商品
     ** @author: 杨陈鹏
     ** @date: 2015-04-12 55:37
     ** @email: yangchenpeng@cdlinglu.com
     */
    public function update()
    {

        $this->id = I('post.id');

        $data = I('post.data','',false);

        if ( $data ) {

            $logic = D('Common/Goods', 'Logic');

            $this->ajaxReturn($logic::update($data,$this->id));

        }


        $category_logic = D('Common/Category', 'Logic');


        $goods_logic = D('Common/Goods','Logic');

        $goods_info = $goods_logic::getOneDatas($this->id);

        $this->goods_info = $goods_info['data'];

        //获取所有规则
        $categorys = $category_logic::getChilds(0, 'id,name,pid,child');

        //获取树型下拉框
        $tree = new \Org\Util\Tree();

        $tree->init($categorys);

        $this->category_tree = $tree->create(0, '<option value=$id $selected $disabled>$spacer $name</option>', $this->goods_info['category_id'], '', true);

        //获取运费模板信息
        $this->transport_info = [];
        if ( $this->goods_info['transport_id'] ){
            $transport_logic = D('Common/Transport','Logic');

            $this->transport_info = $transport_logic::$model
                ->where(['id' => $this->goods_info['transport_id']])
                ->field('id,title')
                ->find();

        }
        //获取默认规格配置
        $this->goods_extends = $this->goods_info['goods_extend'];
        $this->default_spec = [];
        foreach ( $this->goods_extends as  $v)
        {

            if( $v['is_default']  = 1 )
                $this->default_spec = $v;

        }

        foreach ($this->goods_extends as $k => $v) {

            if($v['goods_attr_val_ids']){

                $goods_extends[$v['goods_attr_val_ids']] = $v;


            }

        }

        $this->goods_extends = $goods_extends;

        $this->display();

    }

    /*
     ** @introduction: 加载商品属性
     ** @author: 杨陈鹏
     ** @date: 2015-04-12 55:37
     ** @email: yangchenpeng@cdlinglu.com
     */
    public function getAttrs()
    {

        $category_id = I('post.category_id');

        if (!$category_id)
            $this->ajaxReturn(['code' => 300, 'msg' => '参数错误']);

        $logic = D('Common/Attr', 'Logic');

        $this->data = $logic::getData(['category_id' => $category_id]);

        if ($this->data['code'] == 300)
            $this->ajaxReturn($this->data);

        $this->data = $this->data['data'];

        $this->display();


    }

    /**
     * Introduction: 获取运费模板
     * @author: 杨陈鹏
     * @date: 2016/4/12 13:31
     * @email: yangchenpeng@cdlinglu.com
     */
    public function getTransport()
    {

        $logic = D('Common/Transport', 'Logic');

        $data = $logic::getData(['stroe_id' => 0]);

        $this->data = $data['data'];

        $this->display();
    }

    /**
     * @introduction: 根据商品ID获取规格商品
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    public function getSpecLists()
    {

        $goods_id = I('post.goods_id');

        $logic = D('Common/Goods','Logic');

        $data = $logic::getSpecLists($goods_id);

        $this->ajaxReturn($data);

    }
    /**
     * @introduction: 商品上架下架
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    public function updateStatus()
    {

        $action= I('post.action');

        $ids = I('post.ids');

        if(!$ids)
            $this->ajaxReturn(['code' => 300, 'msg' => '请至少选择一个商品!']);

        $logic = D('Common/Goods','Logic');

        $ids = implode(',',$ids);

        $where = "id IN($ids)";

        //上架
        if($action == 'up'){

            $data['publish_time'] = NOW_TIME - 1;
            $data['goods_state'] = 1;

        }

        //下架
        if($action == 'down')
            $data['goods_state'] = 0;

        //删除
        if($action == 'delete')
            $data['goods_state'] = 2;

        $result = $logic::$model
                        ->where($where)
                        ->save($data);

        if($result !== false)
            $this->ajaxReturn(['code' => 200, 'msg' => '操作成功!']);

        $this->ajaxReturn(['code' => 300, 'msg' => '操作失败!']);


    }
}